$(function() {  
    $("#product_lists li").click(function() {
        $("#product_lists li").removeClass("active");
        $(this).addClass("active");
        var data = $(this).attr("data");
        $("#product_id").val(data);
    });

    $("#month_lists li").click(function() {
        $("#month_lists li").removeClass("active");
        $(this).addClass("active");
        var data = $(this).attr("data");
        $("#month").val(data);
        if (data == 1) {
            $("#last_money").text("88.00");
            $("#price_notice").text("");
        } else {
            $("#last_money").text("528.00");
            $("#price_notice").text("/5折");
        }
    });
});

function wechatPay() {
    var userAgent = navigator.userAgent;
    var isMini = /miniProgram/i.test(userAgent);
    if(isMini){
        mviewToastr("error",'小程序不支持付费，请前往天天外链公众号或PC官网操作！');
        return;
    }
    var product_id = $("#product_id").val();
    var month = $("#month").val();
    var payment_id = $("#payment_id").val();
    var type = $("#type").val();
    var wechat = isWeChat() && !isPCWeChat() ? 1 : 2;
    $.ajax({
        url: '/admin/order/pay',
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        type: 'POST',
        dataType: 'json',
        data: {
            "product_id":product_id,
            "month":month,
            "payment_id":payment_id,
            "type":type,
            "wechat":wechat,
        },
        success:function (result) {
            if (result.status == "success") {
                if (wechat == 1 && !isPCWeChat()) {
                    WeixinJSBridge.invoke('getBrandWCPayRequest',result.data,(res)=>{
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            mviewToastr("success","支付成功,即将跳转订单列表！");
                            setTimeout(function(){
                                window.location.href = "/admin/links_lists/member_orders";
                            },1500);
                        }
                    });
                } else {
                    $("#qrcode").html("");
                    $("#out_trade_no").val(result.data.out_trade_no);
                    $(".wechat-pay-cotain-notice h3 font").text(result.data.price);
                    $(".wechat-pay-cotain-notice h5 font").text(result.data.month);
                    var qrcode = new QRCode(document.getElementById("qrcode"), {
                        text: result.data.code_url,
                        width: 120,
                        height: 120,
                        colorDark : "#000000",
                        colorLight : "#ffffff",
                        correctLevel : QRCode.CorrectLevel.H
                    });
                    $(".wechat-pay-cotain").show();
                    $("#qrcode").show();
                    checkPay();
                }
            } else {
                mviewToastr("error",result.msg);
            }
        },
        error:function (err){
            mviewToastr("error","网络异常,请联系客服处理！");
        }
    });
}

function isWeChat() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("micromessenger") !== -1;
}

function isPCWeChat(){
    var ua = navigator.userAgent.toLowerCase();
    return ua.indexOf("macwechat") !== -1 || ua.indexOf("windowswechat") !== -1;
}

function checkPay() {
    var refreshtimes = 1;
    refresh = setInterval(function(){
        refreshtimes += 1;
        checkWechatPay();
        // 5分钟后过期
        if (refreshtimes >= 24) {
            $(".wechat-pay-cotain").hide();
            clearInterval(refresh);
        }
    },5000);
}

function checkWechatPay() {
    var out_trade_no = $("#out_trade_no").val();
    $.ajax({
        url: '/admin/order/wechat_pay_status',
        type: 'GET',
        dataType: 'json',
        data: {
            "out_trade_no":out_trade_no,
        },
        success:function (result) {
            if (result.status == "success") {
                window.location.href = "/admin/alipay/mini";
            }
        },
        error:function (err){
            mviewToastr("error","网络异常,请联系客服处理！");
        }
    });
}
